/************************************************************************
 * @file SiteManager.cpp
 * @author Ian Chen
 * @version 1.0
 *
 * @section LICENSE
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 3 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
 * General Public License for more details at
 * http://www.gnu.org/licenses/lgpl.html
 *
 ************************************************************************/
#include <QDir>
#include <QDomDocument>
#include <QDomElement>
#include <QDomNode>
#include <QDomNodeList>
#include <QDomText>
#include <QFileInfo>
#include <QFile>
#include <QTextStream>

#include <plugin/util/SiteManager.h>
#include <plugin/util/Framework.h>
#include <plugin/plugin/PluginContext.h>

namespace antu
{
namespace plugin
{
SiteManager::SiteManager()
{
}

vector<SiteInfoStruct> SiteManager::sites()
{
    QFile file(this->siteFile());
    if (file.exists())
    {
        vector<SiteInfoStruct> result;
        QDomDocument doc;
        doc.setContent(&file);
        QDomNodeList list = doc.elementsByTagName("site");

        for (int i = 0; i < list.size(); i++)
        {
            QDomNode node = list.at(i);
            if (node.isElement())
            {
                QDomElement ele = node.toElement();
                QString name = ele.attribute("name");
                QString url = ele.attribute("url");

                SiteInfoStruct info;
                info.name = name;
                info.url = url;
                result.push_back(info);
            }
        }
        return result;
    }
    else
    {
        return vector<SiteInfoStruct>();
    }
}

void SiteManager::addSite(const QString &name, const QString &url)
{
    QFile file(this->siteFile());

    if (!file.exists())
    {
        QDomDocument doc("sites");
        QDomElement root = doc.createElement("sites");
        doc.appendChild(root);
        QDomElement site = doc.createElement("site");
        root.appendChild(site);

        site.setAttribute("name", name);
        site.setAttribute("url", url);

        QTextStream st(&file);
        doc.save(st, 4);
    }
    else
    {
        QDomDocument doc;
        doc.setContent(&file);
        QDomElement root = doc.documentElement();
        QDomElement site = doc.createElement("site");
        root.appendChild(site);

        site.setAttribute("name", name);
        site.setAttribute("url", url);
        QTextStream st(&file);
        doc.save(st, 4);
    }
}

void SiteManager::removeAt(size_t index)
{
    QFile file(this->siteFile());

    if (file.exists())
    {
        QDomDocument doc;
        doc.setContent(&file);
        QDomElement root = doc.documentElement();

        for (int i = 0, j = 0; i < root.childNodes().count(); i++)
        {
            QDomNode child = root.childNodes().at(i);
            if (child.isElement())
            {
                QDomElement childEle = child.toElement();
                if (childEle.tagName() == "site")
                {
                    if (j < index)
                    {
                        j++;
                    }
                    else if (index == j)
                    {
                        root.removeChild(child);
                        break;
                    }
                }
            }
        }

        QTextStream st(&file);
        doc.save(st, 4);
    }
}

QString SiteManager::siteFile() const
{
    QString path = Framework::Instance.context()->pluginPath();
    QFileInfo info(QDir(path), "sites.xml");
    return info.absoluteFilePath();
}

}
}

